package com.ruoyi.web.controller.devops;

import cn.hutool.core.io.resource.ClassPathResource;
import com.ruoyi.analysiswarning.domain.ScriptApp;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.devops.domain.*;
import com.ruoyi.devops.domain.vo.CheckVo;
import com.ruoyi.devops.domain.vo.HisReportVo;
import com.ruoyi.devops.service.impl.DevopsService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import static com.ruoyi.common.core.domain.AjaxResult.success;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/devopscontroller")
public class DevopsController {

    @Autowired
    DevopsService devopsService;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields(new String[]{"admin"});
    }

    // 查询硬件
    @Log(title = "查询硬件", businessType = BusinessType.QUERY)
    @PostMapping("queryDeviceInfo")
    public AjaxResult queryDeviceInfo() {
        List<DeviceInfo> list = devopsService.queryDeviceInfo();
        return success(list);
    }

    // 查询软件
    @Log(title = "查询软件", businessType = BusinessType.QUERY)
    @PostMapping("querySoftwareInfo")
    public AjaxResult querySoftwareInfo() {
        List<SoftwareInfo> list = devopsService.querySoftwareInfo();
        return success(list);
    }

    // 查询历史报告
    @Log(title = "查询历史报告", businessType = BusinessType.QUERY)
    @PostMapping("queryHistoryReports")
    public AjaxResult queryHistoryReports(@RequestBody Map<String,String> map) {
        return devopsService.queryHistoryReports(map);

    }


    // 查询历史报告
    @Log(title = "插入历史报告", businessType = BusinessType.QUERY)
    @PostMapping("insertHistoryReports")
    public AjaxResult insertHistoryReports(@RequestBody HisReportVo hisReportVo) {
        Integer i= devopsService.insertHistoryReports(hisReportVo);
        return success(i);
    }


     //导出历史报告Excel
//    @PostMapping("exportHisReports")
//    public void exportHisReports(HttpServletResponse response, HisReportVo hisReportVo) {
//
//        List<HistoryReports> list = devopsService.queryHistoryReports(hisReportVo);
//        ExcelUtil<HistoryReports> util = new ExcelUtil<>(HistoryReports.class);
//        util.exportExcel(response, list, "历史报告");
//    }

    @PostMapping("exportHisReports")
    public AjaxResult exportHisReports(@RequestBody HisReportVo hisReportVo) {
        return devopsService.exportHisReports(hisReportVo);
    }

    // 合规性检查
    @Log(title = "合规性检查", businessType = BusinessType.QUERY)
    @PostMapping("comcheck")
    public AjaxResult comcheck(@RequestBody CheckVo checkVo) {
        Map map = devopsService.comcheck(checkVo);
        return success(map);
    }

    /** 新增指标采集脚本 **/
    @Log(title = "新增指标采集脚本", businessType = BusinessType.QUERY)
    @PostMapping("insertIndexScript")
    public AjaxResult InsertIndexScript(@RequestBody IndexScript indexScript) {
        return success(devopsService.InsertIndexScript(indexScript));
    }



    // 查询合规检查模板
    @Log(title = "查询合规检查模板", businessType = BusinessType.QUERY)
    @PostMapping("queryComTemplate")
    public AjaxResult queryComTemplate(String id, String name) {
        List<ComTemplate> list = devopsService.queryComTemplate(id, name);
        return success(list);
    }

    // 删除合规检查模板
    @Log(title = "删除合规检查模板", businessType = BusinessType.DELETE)
    @PostMapping("deleteComTemplate")
    public AjaxResult deleteComTemplate(String id) {
        return success(devopsService.deleteComTemplate(id));
    }

    // 更新合规检查模板
    @Log(title = "更新合规检查模板", businessType = BusinessType.UPDATE)
    @PostMapping("updateComTemplate")
    public AjaxResult udpateComTemplate(@RequestBody ComTemplate comTemplate) {
        return success(devopsService.updateComTemplate(comTemplate));
    }

    // 新增合规检查模板
    @Log(title = "新增合规检查模板", businessType = BusinessType.INSERT)
    @PostMapping("addComTemplate")
    public AjaxResult addComTemplate(@RequestBody ComTemplate comTemplate) {
        comTemplate.setState("1");
        comTemplate.setIsdelete("0");
        return success(devopsService.addComTemplate(comTemplate));
    }

    // 启用合规检查模板
    @PostMapping("enableComTemplate")
    public AjaxResult enableComTemplate(String id) {
        devopsService.enableComTemplate(id);
        return success();
    }

    // 启用合规检查模板
    @PostMapping("enableComTemplate2")
    public AjaxResult enableComTemplate2(String id) {
        devopsService.enableComTemplate2(id);
        return success();
    }

    // 查询启用的合规检查模板
    @Log(title = "查询软件系统管理", businessType = BusinessType.QUERY)
    @PostMapping("queryEnableComTemplate")
    public AjaxResult queryEnableComTemplate() {
        List<ComTemplate> list = devopsService.queryEnableComTemplate();
        return success(list);
    }

    // 查询合规检查模块
    @Log(title = "查询合规检查模块", businessType = BusinessType.QUERY)
    @PostMapping("queryComModule")
    public AjaxResult queryComModule(String id, String name) {
        List<ComModule> list = devopsService.queryComModule(id, name);
        return success(list);
    }

    // 删除合规检查模块
    @Log(title = "删除合规检查模块", businessType = BusinessType.DELETE)
    @PostMapping("deleteComModule")
    public AjaxResult deleteComModule(String id) {
        return success(devopsService.deleteComModule(id));
    }

    // 更新合规检查模块
    @PostMapping("updateComModule")
    @Log(title = "更新合规检查模块", businessType = BusinessType.UPDATE)
    public AjaxResult udpateComModule(@RequestBody ComModule comModule) {
        return success(devopsService.updateComModule(comModule));
    }

    // 新增合规检查模块
    @Log(title = "新增合规检查模块", businessType = BusinessType.INSERT)
    @PostMapping("addComModule")
    public AjaxResult addComModule(@RequestBody ComModule comModule) {
        comModule.setIsdelete("0");
        return success(devopsService.addComModule(comModule));
    }

    // 查询检查项
    @Log(title = "查询检查项", businessType = BusinessType.QUERY)
    @PostMapping("queryCheckDep")
    public AjaxResult queryCheckDep(String id, String name, String isadmin) {
        List<CheckDep> list = devopsService.queryCheckDep(id, name, isadmin);
        return success(list);
    }

    // 删除检查项
    @Log(title = "删除检查项", businessType = BusinessType.DELETE)
    @PostMapping("deleteCheckDep")
    public AjaxResult deleteCheckDep(String id) {
        return success(devopsService.deleteCheckDep(id));
    }

    // 更新检查项
    @Log(title = "更新检查项", businessType = BusinessType.UPDATE)
    @PostMapping("updateCheckDep")
    public AjaxResult udpateCheckDep(@RequestBody CheckDep checkDep) {
        return success(devopsService.updateCheckDep(checkDep));
    }

    // 新增检查项
    @Log(title = "新增检查项", businessType = BusinessType.INSERT)
    @PostMapping("addCheckDep")
    public AjaxResult addCheckDep(@RequestBody CheckDep checkDep) {
        checkDep.setIsdelete("0");
        return success(devopsService.addCheckDep(checkDep));
    }

    @PostMapping("/addHistory")
    public AjaxResult addHistoryR(@RequestBody ScriptApp app){
        return devopsService.addHistoryR(app);
    }


    // 审核检查项
    @PostMapping("checkCheckDep")
    public AjaxResult checkCheckDep(String id) {
        return success(devopsService.checkCheckDep(id));
    }

    // 查询漏洞
    @Log(title = "查询漏洞", businessType = BusinessType.QUERY)
    @PostMapping("queryHole")
    public AjaxResult queryHole() {
        return success(devopsService.queryDsHole());
    }

    //导出漏洞Excel
    @PostMapping("exportHole")
    public void exportHisReports(HttpServletResponse response) {
        List<DsHole> list = devopsService.queryDsHole();
        ExcelUtil<DsHole> util = new ExcelUtil<>(DsHole.class);
        util.exportExcel(response, list, "历史报告");
    }


    //导出漏洞扫描Excel
    @PostMapping("exportOneHole")
    public void exportOneHole(HttpServletResponse response) throws IOException {
        List<DsHole> list = devopsService.queryDsHole();
        DsHole dsHole = list.get(0);
        //取时间


        //将你创建好的excel文件放到静态文件夹下面然后引用
        ClassPathResource classPathResource = new ClassPathResource("static/321.xls");
        InputStream inputStream = classPathResource.getStream();
        HSSFWorkbook workBook = new HSSFWorkbook(inputStream);


        HSSFSheet sheet1 = workBook.getSheetAt(0);//获取第一个sheet页，也就是正常填充业务数据的sheet页


        HSSFRow row1 = sheet1.getRow(1);
        row1.createCell(3);
        HSSFCell cell = row1.getCell(3);
        cell.setCellValue(dsHole.getTime());

        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("表名.xls", "UTF-8"));
        ServletOutputStream out = response.getOutputStream();
        workBook.write(out);
        out.close();
        workBook.close();




    }


    // 漏洞扫描
    @PostMapping("searchHole")
    public AjaxResult searchHole() {
        return success(devopsService.searchDsHole());
    }

    // 查询指标采集脚本
    @Log(title = "查询指标采集脚本", businessType = BusinessType.QUERY)
    @PostMapping("queryIndexScript")
    public AjaxResult queryIndexScript(String id,String stat) {
        return success(devopsService.queryIndexScript(id,stat));
    }




    // 审核指标采集脚本
    @PostMapping("checkIndexScript")
    public AjaxResult checkIndexScript(String id) {
        return success(devopsService.checkIndexScript(id));
    }




}
